package com.xiangha.springboot.controller;

import com.xiangha.springboot.pojo.Users;
import com.xiangha.springboot.service.impl.UsersServiceImpl;
import com.xiangha.springboot.utils.JwtUtils;
import com.xiangha.springboot.utils.ResponseMsg;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author itlong
 * @since 2025-09-19 03:40:35
 */
@RestController
public class UsersController {
    @Autowired
    UsersServiceImpl usersService;
    @RequestMapping("/user/profile")
    ResponseMsg getUser(HttpServletRequest request){
        String token = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        System.out.println("当前登录的用户信息:" + claims.getSubject());
        Users user = new Users();
        user.setUserId(Integer.parseInt(claims.getId()));
        Integer userId = user.getUserId();
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "获取用户信息成功", usersService.getById(userId));
    }
    @RequestMapping("/user/upload-avatar")
    ResponseMsg uploadAvatar(HttpServletRequest request) {
        String token = request.getHeader("token");
        System.out.println(111+token+111);
        try {
            Claims claims = JwtUtils.parseJWT(token);
            System.out.println("当前登录的用户信息:" + claims.getSubject());
            Users user = new Users();
            user.setUserId(Integer.parseInt(claims.getId()));
            Integer userId = user.getUserId();
            user = usersService.getById(userId);
            System.out.println("当前登录的用户信息:" + user);

            // 获取头像参数
            String avatar = request.getParameter("avatar");
            System.out.println("上传的头像地址:" + avatar);

            // 验证参数不为空
            if (avatar == null || avatar.isEmpty()) {
                return ResponseMsg.getInstance(ResponseMsg.ERROR, "头像地址不能为空");
            }

            // 验证URL格式（简单示例）
            if (!avatar.matches("^https?://.*\\.(jpg|jpeg|png|gif)(\\?.*)?$")) {
                return ResponseMsg.getInstance(ResponseMsg.ERROR, "头像地址格式不正确");
            }

            // 添加文件大小检查逻辑（如果avatar是实际上传的文件）
            // 这里假设avatar是文件URL，实际应用中可能需要根据具体实现调整

            user.setAvatar(avatar);
            System.out.println("上传的头像地址:" + user.getAvatar());
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "上传头像成功", usersService.updateById(user));
        } catch (ExpiredJwtException e) {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "登录已过期，请重新登录");
        } catch (Exception e) {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "token无效，请重新登录");
        }
    }

    @RequestMapping ("/user/update-nickname")
    ResponseMsg updateNickname(@RequestBody Users users,HttpServletRequest request) {
        String token = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        users.setUserId(Integer.parseInt(claims.getId()));
        System.out.println("user消息："+users);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "修改昵称成功", usersService.updateById(users));
    }
}
